package system

import (
  "livefind/common/request"
  "livefind/common/response"
  "livefind/global"
  "livefind/middleware"
  "livefind/model/system"
  systemReq "livefind/model/system/request"
  systemRes "livefind/model/system/response"
  "livefind/utils"
  "strconv"
  "time"

  "github.com/dgrijalva/jwt-go"
  "github.com/gin-gonic/gin"
  "github.com/go-redis/redis/v8"
  "go.uber.org/zap"
)

// @Tags Base
// @Summary 用户登录
// @Produce  application/json
// @Param data body systemReq.Login true "用户名, 密码, 验证码"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}"
// @Router /base/login [post]
func (b *BaseApi) Login(c *gin.Context) {
  var l systemReq.Login
  _ = c.ShouldBindJSON(&l)
  if err := utils.Verify(l, utils.LoginVerify); err != nil {
    response.FailWithMessage(err.Error(), c)
    return
  }
  if store.Verify(l.CaptchaId, l.Captcha, true) {
    u := &system.SysUser{Username: l.Username, Password: l.Password}
    if err, user := userService.Login(u); err != nil {
      global.GVA_LOG.Error("登陆失败! 用户名不存在或者密码错误!", zap.Any("err", err))
      response.FailWithMessage("用户名不存在或者密码错误", c)
    } else {
      b.tokenNext(c, *user)
    }
  } else {
    response.FailWithMessage("验证码错误", c)
  }
}

// 登录以后签发jwt
func (b *BaseApi) tokenNext(c *gin.Context, user system.SysUser) {
  j := &middleware.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名
  claims := systemReq.CustomClaims{
    UUID:        user.UUID,
    ID:          user.ID,
    NickName:    user.NickName,
    Username:    user.Username,
    AuthorityId: user.AuthorityId,
    BufferTime:  global.GVA_CONFIG.JWT.BufferTime, // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失
    StandardClaims: jwt.StandardClaims{
      NotBefore: time.Now().Unix() - 1000,                              // 签名生效时间
      ExpiresAt: time.Now().Unix() + global.GVA_CONFIG.JWT.ExpiresTime, // 过期时间 7天  配置文件
      Issuer:    "qmPlus",                                              // 签名的发行者
    },
  }
  token, err := j.CreateToken(claims)
  if err != nil {
    global.GVA_LOG.Error("获取token失败!", zap.Any("err", err))
    response.FailWithMessage("获取token失败", c)
    return
  }
  if !global.GVA_CONFIG.System.UseMultipoint {
    response.OkWithDetailed(systemRes.LoginResponse{
      User:      user,
      Token:     token,
      ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
    }, "登录成功", c)
    return
  }
  if err, jwtStr := jwtService.GetRedisJWT(user.Username); err == redis.Nil {
    if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
      global.GVA_LOG.Error("设置登录状态失败!", zap.Any("err", err))
      response.FailWithMessage("设置登录状态失败", c)
      return
    }
    response.OkWithDetailed(systemRes.LoginResponse{
      User:      user,
      Token:     token,
      ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
    }, "登录成功", c)
  } else if err != nil {
    global.GVA_LOG.Error("设置登录状态失败!", zap.Any("err", err))
    response.FailWithMessage("设置登录状态失败", c)
  } else {
    var blackJWT system.JwtBlacklist
    blackJWT.Jwt = jwtStr
    if err := jwtService.JsonInBlacklist(blackJWT); err != nil {
      response.FailWithMessage("jwt作废失败", c)
      return
    }
    if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
      response.FailWithMessage("设置登录状态失败", c)
      return
    }
    response.OkWithDetailed(systemRes.LoginResponse{
      User:      user,
      Token:     token,
      ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
    }, "登录成功", c)
  }
}

// @Tags SysUser
// @Summary 用户注册账号
// @Produce  application/json
// @Param data body systemReq.Register true "用户名, 昵称, 密码, 角色ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"注册成功"}"
// @Router /user/register [post]
func (b *BaseApi) Register(c *gin.Context) {
  var r systemReq.Register
  _ = c.ShouldBindJSON(&r)
  if err := utils.Verify(r, utils.RegisterVerify); err != nil {
    response.FailWithMessage(err.Error(), c)
    return
  }
  var authorities []system.SysAuthority
  for _, v := range r.AuthorityIds {
    authorities = append(authorities, system.SysAuthority{
      AuthorityId: v,
    })
  }
  user := &system.SysUser{Username: r.Username, NickName: r.NickName, Password: r.Password, HeaderImg: r.HeaderImg, AuthorityId: r.AuthorityId, Authorities: authorities}
  err, userReturn := userService.Register(*user)
  if err != nil {
    global.GVA_LOG.Error("注册失败!", zap.Any("err", err))
    response.FailWithDetailed(systemRes.SysUserResponse{User: userReturn}, "注册失败", c)
  } else {
    response.OkWithDetailed(systemRes.SysUserResponse{User: userReturn}, "注册成功", c)
  }
}

// @Tags SysUser
// @Summary 用户修改密码
// @Security ApiKeyAuth
// @Produce  application/json
// @Param data body systemReq.ChangePasswordStruct true "用户名, 原密码, 新密码"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
// @Router /user/changePassword [post]
func (b *BaseApi) ChangePassword(c *gin.Context) {
  var user systemReq.ChangePasswordStruct
  _ = c.ShouldBindJSON(&user)
  if err := utils.Verify(user, utils.ChangePasswordVerify); err != nil {
    response.FailWithMessage(err.Error(), c)
    return
  }
  u := &system.SysUser{Username: user.Username, Password: user.Password}
  if err, _ := userService.ChangePassword(u, user.NewPassword); err != nil {
    global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
    response.FailWithMessage("修改失败，原密码与当前账户不符", c)
  } else {
    response.OkWithMessage("修改成功", c)
  }
}

// @Tags SysUser
// @Summary 分页获取用户列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.PageInfo true "页码, 每页大小"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /user/getUserList [post]
func (b *BaseApi) GetUserList(c *gin.Context) {
  var pageInfo request.PageInfo
  _ = c.ShouldBindJSON(&pageInfo)
  if err := utils.Verify(pageInfo, utils.PageInfoVerify); err != nil {
    response.FailWithMessage(err.Error(), c)
    return
  }
  if err, list, total := userService.GetUserInfoList(pageInfo); err != nil {
    global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
    response.FailWithMessage("获取失败", c)
  } else {
    response.OkWithDetailed(response.PageResult{
      List:     list,
      Total:    total,
      Page:     pageInfo.Page,
      PageSize: pageInfo.PageSize,
    }, "获取成功", c)
  }
}

// @Tags SysUser
// @Summary 更改用户权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body systemReq.SetUserAuth true "用户UUID, 角色ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
// @Router /user/setUserAuthority [post]
func (b *BaseApi) SetUserAuthority(c *gin.Context) {
  var sua systemReq.SetUserAuth
  _ = c.ShouldBindJSON(&sua)
  if UserVerifyErr := utils.Verify(sua, utils.SetUserAuthorityVerify); UserVerifyErr != nil {
    response.FailWithMessage(UserVerifyErr.Error(), c)
    return
  }
  userID := utils.GetUserID(c)
  uuid := utils.GetUserUuid(c)
  if err := userService.SetUserAuthority(userID, uuid, sua.AuthorityId); err != nil {
    global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
    response.FailWithMessage(err.Error(), c)
  } else {
    claims := utils.GetUserInfo(c)
    j := &middleware.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名
    claims.AuthorityId = sua.AuthorityId
    if token, err := j.CreateToken(*claims); err != nil {
      global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
      response.FailWithMessage(err.Error(), c)
    } else {
      c.Header("new-token", token)
      c.Header("new-expires-at", strconv.FormatInt(claims.ExpiresAt, 10))
      response.OkWithMessage("修改成功", c)
    }

  }
}

// @Tags SysUser
// @Summary 设置用户权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body systemReq.SetUserAuthorities true "用户UUID, 角色ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
// @Router /user/setUserAuthorities [post]
func (b *BaseApi) SetUserAuthorities(c *gin.Context) {
  var sua systemReq.SetUserAuthorities
  _ = c.ShouldBindJSON(&sua)
  if err := userService.SetUserAuthorities(sua.ID, sua.AuthorityIds); err != nil {
    global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
    response.FailWithMessage("修改失败", c)
  } else {
    response.OkWithMessage("修改成功", c)
  }
}

// @Tags SysUser
// @Summary 删除用户
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "用户ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /user/deleteUser [delete]
func (b *BaseApi) DeleteUser(c *gin.Context) {
  var reqId request.GetById
  _ = c.ShouldBindJSON(&reqId)
  if err := utils.Verify(reqId, utils.IdVerify); err != nil {
    response.FailWithMessage(err.Error(), c)
    return
  }
  jwtId := utils.GetUserID(c)
  if jwtId == uint(reqId.ID) {
    response.FailWithMessage("删除失败, 自杀失败", c)
    return
  }
  if err := userService.DeleteUser(reqId.ID); err != nil {
    global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
    response.FailWithMessage("删除失败", c)
  } else {
    response.OkWithMessage("删除成功", c)
  }
}

// @Tags SysUser
// @Summary 设置用户信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system.SysUser true "ID, 用户名, 昵称, 头像链接"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
// @Router /user/setUserInfo [put]
func (b *BaseApi) SetUserInfo(c *gin.Context) {
  var user system.SysUser
  _ = c.ShouldBindJSON(&user)
  if err := utils.Verify(user, utils.IdVerify); err != nil {
    response.FailWithMessage(err.Error(), c)
    return
  }
  if err, ReqUser := userService.SetUserInfo(user); err != nil {
    global.GVA_LOG.Error("设置失败!", zap.Any("err", err))
    response.FailWithMessage("设置失败", c)
  } else {
    response.OkWithDetailed(gin.H{"userInfo": ReqUser}, "设置成功", c)
  }
}

// @Tags SysUser
// @Summary 获取用户信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /user/getUserInfo [get]
func (b *BaseApi) GetUserInfo(c *gin.Context) {
  uuid := utils.GetUserUuid(c)
  if err, ReqUser := userService.GetUserInfo(uuid); err != nil {
    global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
    response.FailWithMessage("获取失败", c)
  } else {
    response.OkWithDetailed(gin.H{"userInfo": ReqUser}, "获取成功", c)
  }
}
